package com.baosight.sczx.zm.cp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlan;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanOther;
import com.baosight.sczx.zm.cp.domain.ZMCPProfessionalPlan;

/**
 * <p>
 * Title: ServiceZMCP30.java
 * </p>
 */
public class ServiceZMCP30 extends ServiceEPBase {

	/**
	 * <p>
	 * Function: initLoad
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));// 单位的默认值
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(StaticBlocks.getPlanStatus("cmes.cp.planStatus", true));
		return outInfo;
	}

	/**
	 * <p>
	 * Function: query
	 * </p>
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdPlanType", "一通三防计划", true);
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		StaticTools.setQueryParameters(inInfo, "fdMiningName", UserSession.getInSessionProperty("miningName"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCPProfessionalPlan.query", new ZMCPProfessionalPlan());
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,
				UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		return outInfo;
	}

	/**
	 * <p>
	 * Function: insert
	 * </p>
	 * <p>
	 * Description: 新增
	 * </p>
	 * <p>
	 * return value: ServiceZMCP01
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午03:31:06张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		// 校验是否重复
		EiInfo out = new EiInfo();
		ZMCPProfessionalPlan _dom = new ZMCPProfessionalPlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdPlanYear", _dom.getFdPlanYear());
			params.put("fdPlanMonth", _dom.getFdPlanMonth());
			params.put("fdPlanType", "一通三防计划");
			params.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			boolean validate = this.validate(_dom, "ZMCPProfessionalPlan.query", params, "insert");
			if (validate) {
				out = this.query(inInfo);
				out.setMsg("该年月的一通三防计划已经存在！");
				return out;
			} else {
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				_dom.setFdCreatorId(UserSession.getUserId().toString());
				_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PROFESSIONALPLAN"));
				this.dao.insert("ZMCPProfessionalPlan.insert", _dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}

	/**
	 * <p>
	 * Function: update
	 * </p>
	 * <p>
	 * Description: 修改
	 * </p>
	 * <p>
	 * return value: ServiceZMCP01
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午04:59:23张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCPProfessionalPlan _dom = new ZMCPProfessionalPlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			params.put("fdProfessionalplanId", _dom.getFdKeyId());

			boolean validate = this.validate(_dom, "ZMCPProducePlanYT.query", params, "update");
			if (validate) {
				out = this.query(inInfo);
				out.setMsg("该一通三防计划包含详细计划，不可修改！");
				return out;
			} else {
				params.put("fdPlanYear", _dom.getFdPlanYear());
				params.put("fdPlanMonth", _dom.getFdPlanMonth());
				params.put("fdPlanType", "一通三防计划");
				params.put("fdMiningId", _dom.getFdMiningId());
				validate = this.validate(_dom, "ZMCPProfessionalPlan.updateValidate", params, "update");
				if (validate) {
					out = this.query(inInfo);
					out.setMsg("该年月的一通三防计划已经存在！");
					return out;
				} else {
					_dom.setFdCreatorId(UserSession.getUserId().toString());
					_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());
					_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
					_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
					this.dao.update("ZMCPProfessionalPlan.update", _dom);
				}
			}

		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * <p>
	 * Function: delete
	 * </p>
	 * <p>
	 * Description: 删除
	 * </p>
	 * <p>
	 * return value: ServiceZMCP01
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午04:12:10张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCPProfessionalPlan _dom = new ZMCPProfessionalPlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String> params = new HashMap<String, String>();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdProfessionalplanId", _dom.getFdKeyId());
			boolean validate = this.validate(_dom, "ZMCPProducePlanYT.query", params, "delete");
			if (validate) {
				out = this.query(inInfo);
				out.setMsg("该一通三防计划包含详细计划，不可删除！");
				return out;
			} else {
				this.dao.delete("ZMCPProfessionalPlan.delete", _dom);
			}
		}
		EiInfo outInfo = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}

	/**
	 * <p>
	 * Function: report
	 * </p>
	 * <p>
	 * Description: 上报
	 * </p>
	 * <p>
	 * return value:EiInfo
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2011-10-22上午10:59:40张坤
	 * </p>
	 */
	@SuppressWarnings({ "rawtypes" })
	public EiInfo report(EiInfo inInfo) {
		String fdPlanYear = inInfo.getBlock("result").getCellStr(0, "fdPlanYear");
		String fdPlanMonth = inInfo.getBlock("result").getCellStr(0, "fdPlanMonth");
		
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		_map.put("fdPlanStatus", "草拟");
		
		List result = this.dao.query("ZMCPProfessionalPlan.query", _map);
		//1.0 进度完成
		if (result == null || result.isEmpty()) {
			inInfo.setStatus(-1);
			inInfo = this.query(inInfo);
			inInfo.setMsg(fdPlanYear +"年"+ fdPlanMonth +"月计划已经上报或作废，不能上报！");
			return inInfo;
		}
		
		//判断是否有明细
		Map<String, String> map = new HashMap<String, String>();
		map.put("fdProfessionalplanId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validate = ProducePlanOther.validate(map, "ZMCPProducePlanYT.query");
		if (!validate) {
			inInfo.setStatus(-1);
			inInfo = this.query(inInfo);
			inInfo.setMsg("上报的"+ fdPlanYear +"年"+ fdPlanMonth +"月计划中有的没有明细，请添加明细后上报！");
			return inInfo;
		}
		
		//2.0进度完成
		String fdProduceplanId = ProducePlan.checkCanReport(inInfo);
		//3.0进度完成
		
		if (fdProduceplanId == null) {
			//生产计划已经生效
			inInfo.getBlock("result").setCell(0, "fdPlanStatus", "作废");
			this.update(inInfo, "ZMCPProfessionalPlan.updatePlanStatus");
		}
		else {
			//生产计划不存在主表 或 主表为草拟
			inInfo.getBlock("result").setCell(0, "fdPulisherId", UserSession.getUserId());
			inInfo.getBlock("result").setCell(0, "fdPulisher", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdPlanStatus", "上报");
			this.update(inInfo, "ZMCPProfessionalPlan.updatePlanStatus");
			//4.0进度完成
			
			//5.0进度完成
			ProducePlanOther.clone(fdProduceplanId, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), "T_CP_PRODUCEPLAN_YT", "ZMCPProducePlanYT.queryByClone", "ZMCPProducePlanYT.insert");
		}
		
		EiInfo outInfo = this.query(inInfo);
		if(fdProduceplanId == null) {
			outInfo.setMsg(fdPlanYear +"年"+ fdPlanMonth +"月的生产计划已经发布，上报无效，原纪录作废！");
		}
		else {
			outInfo.setMsg(fdPlanYear +"年"+ fdPlanMonth +"月的采煤计划上报成功！");
		}
		return outInfo;
	}

	/**
	 * <p>
	 * Function: validate
	 * </p>
	 * <p>
	 * Description: 对主表记录新增，删除，上报前的后台校验
	 * </p>
	 * <p>
	 * return value:boolean
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午03:40:23张坤
	 * </p>
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(ZMCPProfessionalPlan _dom, String sqlName, Map<String, String> params, String type) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if (result != null && result.size() > 0 && !"report".equals(type)) {
			return true;
		}
		return false;
	}

}
